
package com.treeman.navi.marker;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.google.android.maps.MapView;
import com.treeman.navi.R;
import com.treeman.navi.marker.ballon.BalloonItemizedOverlay;
import com.treeman.navi.marker.ballon.BalloonOverlayView;
import com.treeman.navi.marker.ballon.CustomBalloonOverlayView;


public class MarkerOverlay extends BalloonItemizedOverlay<MarkerItem>
{

    public final static String TAG = "MarkerOverlay";

    @SuppressWarnings ("unused")
    private Context context = null;

    private ArrayList<MarkerItem> overlays = new ArrayList<MarkerItem>();

    private Bitmap bitmap = null;

    @SuppressWarnings ("unused")
    private Bitmap bitmapToDraw = null;

    private float scale = 1.0f;

    private OnTapListener onTapListener;


    public void setOnTapListener(OnTapListener onTapListener)
    {
	this.onTapListener = onTapListener;
    }

    public void setDefaultMarker(Drawable defaultMarker)
    {

	bitmap = ((BitmapDrawable) defaultMarker).getBitmap();
	bitmapToDraw = bitmap;
    }

    public MarkerOverlay(Drawable defaultMarker, MapView mapView)
    {
	super(boundCenter(defaultMarker), mapView);
	populate();
	setDefaultMarker(defaultMarker);
	this.context = mapView.getContext();
    }

    public float getScale()
    {
	return scale;
    }

    public void setScale(float scale)
    {
	if (scale > 0.0f && scale < 1.0f)
	{
	    this.scale = scale;

	    Matrix matrix = new Matrix();
	    matrix.postScale(scale, scale);

	    bitmapToDraw = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
	}
    }

    @Override
    protected MarkerItem createItem(int i)
    {
	return overlays.get(i);
    }

    @Override
    public int size()
    {
	return overlays.size();
    }

    public void addOverlay(MarkerItem overlay)
    {

	// Log.i(TAG, "Marker item added to overlay.");
	overlays.add(overlay);
	populate();
    }

    @Override
    protected boolean onBalloonTap(int index, MarkerItem item)
    {

	Marker marker = item.getMarker();

	Log.i(TAG, "Marker edit : " + marker.getName() + ", " + marker.getDescription() + " (ID = " + marker.getId() + ")");

	if (onTapListener != null)
	{
	    onTapListener.onTapEvent(marker);
	    hideBalloon();
	}

	return true;

    }

    @Override
    protected boolean onBallonLongTouch(int index, MarkerItem item)
    {
	Marker marker = item.getMarker();

	Log.i(TAG, "Marker delete : " + marker.getName() + ", " + marker.getDescription() + " (ID = " + marker.getId() + ")");

	if (onTapListener != null)
	{
	    onTapListener.onLongTouchEvent(marker);
	    hideBalloon();
	}

	return true;
    }

    @Override
    protected BalloonOverlayView<MarkerItem> createBalloonOverlayView()
    {
	int index = this.getLastFocusedIndex();
	MarkerItem markerItem = this.getItem(index);

	CustomBalloonOverlayView<MarkerItem> balloonOverlayView = new CustomBalloonOverlayView<MarkerItem>(getMapView().getContext(), getBalloonBottomOffset());
	balloonOverlayView.setImage(R.drawable.marker2);

	// use our custom balloon view with our custom overlay item type:
	return balloonOverlayView;
    }


    public interface OnTapListener
    {

	public void onTapEvent(Marker marker);

	public void onLongTouchEvent(Marker marker);
    }
}
